/*! \file imgbtn.cpp
	
	This contains the implementation of the ImageButton class
*/

#include "imgbtn.h"

ImageButton::ImageButton( Component* parent, int imageID, int depressedImageID ) :
	Button( parent, "" ),
	stretchOnResize(false)
{
	if ( depressedImageID == -1 ) {
		depressedImageID = imageID;
	}
	setImageIDs( imageID, depressedImageID );
	ImageHandler::getInstance()->getImageSize( w, h, image );
	//~ cout << "Making image-button of width: " << w << " and height " << h << endl;
}

void ImageButton::setImageIDs( int imageID, int depressedImageID ) {
	image = imageID;
	depressedImage = depressedImageID;
}

void ImageButton::draw() {
	int imageToUse = image;
	if ( depressed || mouseIsDown ) {
		imageToUse = depressedImage;
	}
	
	int screenX = getScreenX(), screenY = getScreenY();
	//~ glLoadIdentity();
	//~ glTranslatef( screenX, screenY, 0.0f );
	
	int textureID = ImageHandler::getInstance()->getWorkingTexture();
	if ( stretchOnResize ) {
		ImageHandler::getInstance()->blit( textureID, imageToUse, 9999.0f, w, h );
	} else {
		ImageHandler::getInstance()->blit( textureID, imageToUse );
	}	
	
	glColor4f( 0.0f, 0.0f, 0.0f, 1.0f );
	//~ glEnable(GL_TEXTURE_2D);
	Font* font = GUI::getInstance()->getFont();
	font->writeString( label, BUTTON_LABEL_PADDING_X, h - font->getMaxAdvanceY() / 2 );	
	if ( border ) {
		ImageHandler::getInstance()->guiBorder( sideID1, cornerID1, screenX, screenY, w, h );
	}
}
